<?php


/*

	Copyright © 2011 Mike Bennett <mike@kereru.com>

	This file is part of Tazu-Time.

    Tazu-Time is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    any later version.

    Tazu-Time is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Tazu-Time.  If not, see <http://www.gnu.org/licenses/>.
    
*/


class edit_xlate  {

	var $title = FALSE;
	var $name = 'edit_xlate';
	var $entry = FALSE;

	var $id = FALSE;
	var $db_file;

	var $default_txt = FALSE;
	var $replace = FALSE;

	function __construct($title="Edit Data Field",$name=FALSE) {
		$this->title = $title;	
		if ( $name ) $this->name = $name;
	}


	function edit($id,$db_file=FALSE) {

		// $id == FALSE add new record

		$this->id = $id;
		$this->db_file = $db_file;
		if ( ! $db_file ) $this->db_file = platform::app_data('Tazu-Timesheet-Manager').'config.db';

//		$cnf = new conf_timesheet(INIFILE);

		$glade = new GladeXML(PATH_RES.'edit_xlate.glade','edit_xlate');
		$glade->signal_autoconnect_instance($this);

		$dialog = $glade->get_widget('edit_xlate');
		$dialog->set_title($this->title);


/*
		$labels = array('name_lbl','company_lbl','email_lbl');
		foreach ( $labels as $lbl ) {
			$w = $glade->get_widget($lbl);
			$w->set_label(settings::xlate($w->get_label()));
		}		
*/


		$this->default_txt = $glade->get_widget('default_txt');
		$this->replace = $glade->get_widget('replace');


		$default_txt = '';
		$replace = '';

		// Edit an existing customer record
		if ( $this->id ) {

			$c = new settings($this->db_file);
			
			$default_txt = $c->getXlateDefault($this->id);
			$replace = $c->getXlateReplace($this->id);
			unset($c);
		}
			
		$this->default_txt->set_text($default_txt);
		$this->replace->set_text($replace);

        $buttons = $dialog->action_area->get_children();
        $button_ok = $buttons[1]; // get the ID of the OK button

        // simulate button click when user press enter
/*
		$this->entry->set_text($default);
        $this->entry->connect('activate', array(&$this, 'on_enter'), $button_ok);
*/



        $dialog->show_all();
		$ok = FALSE;
		while ( ! $ok ) {

        $result = $dialog->run();

		//echo "+ ".Gtk::RESPONSE_OK."\n";
		//echo "- ".Gtk::RESPONSE_CANCEL."\n";
		//echo "->[$result]\n";

		if ( $result == Gtk::RESPONSE_OK ) {

			$replace = trim($this->replace->get_text());

//			if ( $this->id && $timesheet_file <> '' ) {
//				$ok = $this->validate($timesheet_file);
//			}
//			else {
				$ok = TRUE;
//			}
			
			if ( $ok ) {
					
				$c = new settings($this->db_file);

				if ( $this->id ) {
					// Existing record
					$c->updateXlate($this->id,'replace',$replace);							

				}
//				else {
//					// New record
//					$id = $c->addDat($tag, $name, $unit, $description);
//				}
						
				unset( $c );
			}


			$result = $id;

		}
		else {
			$ok = TRUE;
			$result = FALSE;		// cancel
		}

		}

        $dialog->destroy();

		return($result);

    }


	// simulate button click when user press enter
    function on_enter($entry, $button) {
        $button->clicked();
    }



}

?>